//
//  IncomeViewController.m
//  QianJinDai
//
//  Created by wangzijian on 15/10/19.
//  Copyright (c) 2015年 xuxuan. All rights reserved.
//
#import "RemindViewController.h"
#import "RemindCell.h"
#import "Remind.h"
#import "JTMaterialSwitch.h"
#import "AddRemindViewController.h"
@interface RemindViewController ()<UITableViewDataSource,UITableViewDelegate>


@end

@implementation RemindViewController{
    AFHTTPRequestOperationManager *_manager;
    JTMaterialSwitch *_aaSwitch;
    UIButton *_back;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    _manager = [AFHTTPRequestOperationManager manager];
    self.navigationItem.title=@"提醒设置";
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.tableView registerClass:[RemindCell class] forCellReuseIdentifier:@"RemindCell"];
    [self.tableView setSeparatorInset:(UIEdgeInsetsMake(0,0, 0, 0))];
    //[self.tableView initData];
    self.tableView.rowHeight=100;
    
    [self initData];
    [self _createRightItem];
    //__block RemindViewController *controller = self;
//    self.tableView.upBlock=^(void){
//        //NSLog(@"%@",@"333");
//        [controller.tableView initLastData];
//    };
//    self.tableView.pullBlock=^(void){
//        //NSLog(@"%@",@"123321");
//        [controller.tableView initData];
//        
//    };
    
    [super viewDidLoad];
	
    
    
    
    // Do any additional setup after loading the view from its nib.
}
- (void)_createRightItem
{
    UIImage * image = [[UIImage imageNamed:@"add.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    _back = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 20, 20)];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [_back addSubview:imageView];
    [_back addTarget:self action:@selector(add) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *moreButton = [[UIBarButtonItem alloc] initWithCustomView:_back];
    self.navigationItem.rightBarButtonItem = moreButton;
}

-(void)add
{
    AddRemindViewController *addremind = [[AddRemindViewController alloc]init];
    [self.navigationController pushViewController:addremind animated:YES];
}

- (id)init{
    self = [super init];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
- (void)initData
{
    _nilArray = [[NSMutableArray alloc] init];
        for (int i = 0; i<5; i++) {
            Remind *ic = [[Remind alloc]init];
            [ic setRemindId:[NSNumber numberWithInt:i]];
            [ic setRemindTitle:[NSString stringWithFormat:@"%@_%d",@"提醒标题",i]];
            [ic setRemindDesc:@"提醒内容"];
            [ic setRemindDate:[Util getDateTime:@"2016-06-23"]];
            [ic setStatus:1];
            [_nilArray addObject:ic];
        }
    
//    NSString *url=[NSString stringWithFormat:@"%@Records/GetIncomeDetail",BaseURL];
//    NSDictionary *parameters = @{@"memberid":Getmemberid,@"Lastid":@"0",@"pageSize":@"0",@"signature":[Util getSignature:[NSString stringWithFormat:@"%@00",Getmemberid]]};
//    
//    AFHTTPRequestOperation*op= [_manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSError*error;
//        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
//        NSNumber *result = [dic valueForKey:@"Status"];
//        if (error==nil) {
//            if ([result isEqualToNumber:[[NSNumber alloc] initWithInt:1]]) {
//                NSNumber *money=[dic valueForKey:@"Data"];
////                
////                if (error==nil&&![result isEqual:[NSNull null]]&&result!=nil&&result.count!=0) {
////                    for (int i = 0; i<[result count]; i++) {
////                        NSDictionary *data= result[i];
////                        Income *ic = [[Income alloc]init];
////                        [ic setIncomeId:[data valueForKey:@"Id"]];
////                        [ic setIncomeMoney:[[data valueForKey:@"OperateAmount"] doubleValue]];
////                        [ic setIncomeTitle:[NSString stringWithFormat:@"%@",[data valueForKey:@"OperateNum"]]];
////                        [ic setIncomeDesc:[data valueForKey:@"OperateExplain"]];
////                        [ic setIncomeDate:[Util getDateTime:[data valueForKey:@"OperateDate"]]];
////                        [ic setType:[[data valueForKey:@"OperateType"] intValue]];
////                        [_nilArray addObject:ic];
////                    }
////                }
//                //[self.money countFrom:0 to:[money doubleValue]  withDuration:1.5];
//            }else if ([result isEqualToNumber:[[NSNumber alloc] initWithInt:0]]){
//                [[[UIAlertView alloc] initWithTitle: @"网络异常" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
//                
//                return;
//                
//            }
//        }
//        
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        //NSLog(@"网络异常，error = %@",error.localizedDescription);
//    }];
//    op.responseSerializer = [AFCompoundResponseSerializer serializer];
//    [op start];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

//实现TABLE方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _nilArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RemindCell *cell = [RemindCell createWithTableView:tableView];
    
    	
    if (_nilArray!=nil&&_nilArray.count>0) {
        Remind *ic = [_nilArray objectAtIndex:indexPath.row];
        cell.title.text = ic.RemindTitle;
        cell.desc.text = ic.RemindDesc;
        cell.time.text =ic.RemindDate;
        //NSString *title = [ic.RemindTitle substringToIndex:3];
        _aaSwitch = [[JTMaterialSwitch alloc] initWithSize:JTMaterialSwitchSizeNormal
                                                                       style:JTMaterialSwitchStyleDefault
                                                                       state:JTMaterialSwitchStateOn];
        
        _aaSwitch.center = CGPointMake(kScreenWidth-40, 25);
        [_aaSwitch addTarget:self action:@selector(stateChanged) forControlEvents:UIControlEventValueChanged];
        [cell addSubview:_aaSwitch];
        
    }
    return cell;
}
-(void)stateChanged
{
    NSLog(@"~~~~~~~~~");
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_nilArray removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
        
//        NSString *url = [NSString stringWithFormat:@"%@chat/delCollection",BaseURL];
//        NSURL *URL=[NSURL URLWithString:url];//不需要传递参数
//        NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:URL];//默认为get请求
//        request.timeoutInterval=5.0;//设置请求超时为5秒
//        request.HTTPMethod=@"POST";//设置请求方法
//        //设置请求体
//        NSDictionary *d = [_nilArray objectAtIndex:indexPath.row];
//        NSString *param=[NSString stringWithFormat:@"id=%@",d[@"id"]];
//        //把拼接后的字符串转换为data，设置请求体
//        request.HTTPBody=[param dataUsingEncoding:NSUTF8StringEncoding];
//        NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//        NSError *err;
//        NSMutableDictionary * dic =[NSJSONSerialization JSONObjectWithData:received options:NSJSONReadingMutableContainers error:&err];
//        NSString *data = dic[@"code"];
//        if ([data isEqualToString:@"4000"]) {
//            [_nilArray removeObjectAtIndex:indexPath.row];
//            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
//            
//        } else{
//            [[[UIAlertView alloc] initWithTitle: @"删除失败" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
//            
//        }
        
    }
}


@end